package cn.example.vip.mall.canal.listener;

import cn.example.vip.mall.goods.entity.AdItems;
import cn.example.vip.mall.goods.feign.NginxCacheFeign;
import cn.example.vip.mall.goods.feign.SkuFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import java.util.Objects;

/**
 * 数据库操作时触发的处理模块
 */
@Component
@CanalTable("ad_items")
public class AdItemsHandler implements EntryHandler<AdItems> {
	
	@Autowired
	SkuFeign skuFeign;
	@Autowired
	NginxCacheFeign nginxCacheFeign;
	
	/**
	 * 数据库增加,更新缓存
	 * @param adItems
	 */
	@Override
	public void insert(AdItems adItems) {
		skuFeign.updateTypeItems(adItems.getType());
	}
	
	/**
	 * 数据库更新,更新缓存
	 * @param before
	 * @param after
	 */
	@Override
	public void update(AdItems before, AdItems after) {
		if(!Objects.equals(before.getType(), after.getType())){
			System.out.println("---------before:" + before);
			System.out.println("---------after:" + after);
			skuFeign.updateTypeItems(before.getType());	// 重新加载,即便为空也可以更新为空数据
			nginxCacheFeign.updateTypeItems(before.getType());	// 清理Nginx缓存
		}
		skuFeign.updateTypeItems(after.getType());	// 加载新的数据
	}
	
	/**
	 * 数据库删除,更新缓存
	 * @param adItems
	 */
	@Override
	public void delete(AdItems adItems) {
		skuFeign.delTypeItems(adItems.getType());
	}
}
